import { View, Image } from "@tarojs/components";
import { useLoad } from "@tarojs/taro";
import arrowRight from "@/static/arrow_right.png";
import { useBoolean } from "ahooks";
import { ali_pay_url } from "@/api";

import "./index.scss";
export default function Alipay() {
  const [showTip, { setFalse: closeTip, setTrue: openTip }] = useBoolean(false);
  useLoad(async (opts) => {
    console.log("Page loaded.", opts.order_no,opts.token);
    ali_pay_url({
      order_no: opts.order_no,
      token: opts.token
    }).then((payData) => {
      console.log(payData);
      // 判断当前的浏览器环境是不是微信浏览器
      let ua = navigator.userAgent.toLowerCase();
      if (ua.indexOf("micromessenger") !== -1) {
        openTip();
      } else {
        window.location.href = payData.data.url;
        closeTip();
      }
    });
  });
  return (
    <View>
      {showTip ? (
        <View className="alipay-box">
          <Image className="arrow-right" src={arrowRight} />
          <View className="alipay-title">
            支付宝支付请使用浏览器支付，请在菜单中选择浏览器打开
          </View>
        </View>
      ) : (
        ""
      )}
    </View>
  );
}
